

jQuery(function ($) {
    var user = JSON.parse(localStorage.getItem('user_info')) || {};
    if (!user) {
        window.location.href = "/login/login.html";
    }
    var goods_list = JSON.parse(localStorage.getItem('goods_list'));
    var id = user.id;
    if (!id) {
        window.location.href = "/login/login.html";
    }
    //公告
    notice();
    function notice() {
        var user = JSON.parse(localStorage.getItem('user_info')) || {};
        var level_id = user.level_id;
        var user_center_params = {
            "[]": {
                "Notice": {
                    "level_id": level_id
                },
                "count": 4
            }
        };
        var html = "";
        send_ajax('post', '/get', user_center_params, function callback(data) {
            if(data.code==200&&data["[]"]){
                var data = data["[]"];
                for (var i = 0; i < data.length; i++) {
                    html += "<a target=\"_blank\" href=\"/notice/notice_detail.html?id=" + data[i].Notice.id + "\">" + data[i].Notice.title + "</a>";
                }
                $('.notice').html(html);
            }
            
        });
    };
    myOrder();
    function myOrder() {
        var html = "";
        if (id) {
            var param = {
                "YcUser": {
                    "id": id
                },
                "UserLevel[]": {
                    "UserLevel": {
                        "@order": "bottom_point"
                    }
                },
                "UserCoupon[]": {
                    "join": ">/Coupon/id@",
                    "UserCoupon": {
                        "@column": "id",
                        "user_id@": "YcUser/id"
                    },
                    "Coupon": {
                        "id@": "UserCoupon[]/UserCoupon/coupon_id",
                        "end_time%": "2019-06-17 00:00:00,3000-01-01 00:00:00",
                        "use_flag": 1
                    }
                },
                "[]": {
                    "Order": {
                        "@column": "id",
                        "user_id@": "YcUser/id"
                    },
                    "count": 0,
                    "OrderDelivery": {
                        "@column": "id",
                        "order_id@": "[]/Order/id"
                    },
                    "OrderDeliveryGoods": {
                        "delivery_id@": "[]/OrderDelivery/id",
                        "OrderGoods": {
                            "id@": "/goods_id"
                        }
                    }
                }
            };
            send_ajax('post', '/get', param, function (result) {
                if (result.code == 200) {
                    $('#loginName').html(result.YcUser.login_name);
                    if (result.YcUser.portrait) {
                        document.querySelector('#img').src = isImgHttp(result.YcUser.portrait);
                    }
                };
            });
            var myOrderParam={
                userId:id,
                type:0,
                page:1,
                size:10
            }
            send_ajax('get', '/api/order/myOrder', myOrderParam, function (data) {
                if(data.code==200&&data.data.orderList.length){
                    var data = data.data.orderList;
                    for(var i=0;i<data.length;i++){
                        var a_data = data[i].goodsList;
                        var order = data[i].order;
                        for(var n = 0;n<a_data.length;n++){
                            var a_good = a_data[n];
                             html += "\n<div class=\"details\">\
                            <div class=\"left\">\
                            <a href=\"/goods-" + a_good.id + ".html\" target=\"_blank\">\
                            <img src=\"" + (a_good.thumb ? isImgHttp(a_good.thumb) : '/asset/image/tu4.jpg') + "\" alt=\" " + a_good.goods_name + "\">\n </a>  \
                            </div>\
                            <div class=\"right\">\
                            <p class=\"goods_msg\"> <a href=\"/goods-" + a_good.id + ".html\" target=\"_blank\">\ "+a_good.goods_name+" </a>\n   </p>\<p class=\"time\">" +formatDate(new Date(a_good.create_time))  + "</p>\</div>\
                            <div id=\"order_details\">\ <a href=\"/user/order_details.html?order_id=" + order.id + '" target="_blank">\u67E5\u770B\u8BE6\u60C5</a>\</div>\ </div> ';
                        }
                       
                    };
                    $('.order_list').html(html);
                }
            });    
        }
    };
    function formatDate(now) { 
        var year=now.getFullYear(); 
        var month=now.getMonth()+1; 
        var date=now.getDate(); 
        var hour=now.getHours(); 
        var minute=now.getMinutes(); 
        var second=now.getSeconds(); 
        return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second; 
    } 
    //推荐字画
    var params = {
        "count":2
    };
    send_ajax('get', '/api/goods/recomend', params, function callback(res) {
        var html = "";
        if (res.code == 200&&res.data.length) {
            var data = res.data;
            for (var i = 0; i < data.length; i++) {
                var a_data = data[i];
                    html += '<div>\n<a target="_blank" href="/goods-' + a_data.id + '.html"><img src="' + isImgHttp(a_data.thumb) + '" alt=""></a>  \n                        <p><a target="_blank" href="/goods-' + a_data.id + '.html" class="picMsg">' + a_data.name + '</a>  </p>\n                        <span>\uFFE5' + a_data.goods_price + '</span>\n                    </div>';
            }
            $('.tuijian_box .details').html(html);
        }
    });
  
    //我的收藏
    var storeParam = {
        "[]": {
            "count": 2,
            "GoodsCollect": {
                "user_id": id
            },
            "Goods": {
                "id@": "[]/GoodsCollect/goods_id"
            },
            "GoodsImgs": {
                "goods_id@": "[]/GoodsCollect/goods_id"
            }
        }
    };
    send_ajax('post', '/get', storeParam, function callback(res) {
        var html = "";
        if (res.code == 200) {
            if (res["[]"]) {
                var data = res["[]"];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].Goods) {
                        html += '<div class="small">\n<a href="/goods-' + data[i].Goods.id + '.html" target="_blank"><img src="' + isImgHttp(data[i].GoodsImgs.thumb) + '" alt=""></a>  \n                     <a href="/goods-' + data[i].Goods.id + '.html" target="_blank" class="goods_name"><span>' + data[i].Goods.name + '</span></a> \n                            <span>\xA5' + data[i].Goods.goods_price +'/'+data[i].Goods.unit_name+ '</span>\n                        </div>';
                    }
                }
                $('#store').html(html);
                $('#noStore').hide();
            } else {
                $('#store').hide();
                $('#noStore').show();
            }
        }
    });
    //我的足迹
    if (goods_list != null) {
        var arr = [];
        var footprint = "";
        for (var k = 0; k < goods_list.length; k++) {
            // if (k <= 1) {
                arr.push(goods_list[k].id);
            // }
        }
        var footParam = {
            "[]": {
                "Goods": {
                    "id{}": arr
                },
                "GoodsImgs": {
                    "goods_id@": "[]/Goods/id",
                    "main_flag": 1
                }
            }
        };
        send_ajax('post', '/get', footParam, function callback(res) {
            if (res.code == 200&&res["[]"]) {
                var data = res["[]"];
                 for (var i = 0; i < data.length; i++) {
                    footprint += '<div class="small">\n                                <a href="/goods-' + data[i].Goods.id + '.html" target="_blank"><img src="' + isImgHttp(data[i].GoodsImgs.thumb) + '" alt=""></a>  \n                           <a href="/goods-' + data[i].Goods.id + '.html" target="_blank" class="goods_name"><span>' + data[i].Goods.name + '</span></a> \n                                <span>\xA5' + data[i].Goods.goods_price +'/'+data[i].Goods.unit_name+ '</span>\n                            </div>';
                }
                $('.footprint').html(footprint);
            }
           
        });
    } else {
        $('.footprint').html('还没有浏览足迹');
    }
    //我的足迹点击查看更多
    $('#myFoot').click(function () {
        if ($(this).parents('.zuji_box').find('.bottom').hasClass('myprint')) {
            $(this).parents('.zuji_box').find('.bottom').removeClass('myprint');
        } else {
            $(this).parents('.zuji_box').find('.bottom').addClass('myprint');
        }
    });
});